package org.colin.controller;


import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.*;
import org.colin.aop.annotation.LogAnnotation;
import org.colin.code.BaseResponseCode;
import org.colin.config.ConfigCode;
import org.colin.dictPojo.DSubject;
import org.colin.dictPojo.DSubjectType;
import org.colin.service.DSubjectService;
import org.colin.service.DSubjectTypeService;
import org.colin.utils.DataResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 试题标签表 前端控制器
 * </p>
 *
 * @author LiRuiFeng
 * @since 2021-01-09
 */
@Controller
@Api(tags = "基础数据管理-试题标签管理")
@RequestMapping("/dSubjectType")
public class DSubjectTypeController {


    @Autowired
    DSubjectTypeService dSubjectTypeService;
    @Autowired
    DSubjectService dSubjectService;


    @ApiOperation(value = "新增试题标签接口")
    @LogAnnotation(title = "基础数据管理", action = "新增试题标签接口")
    @PostMapping("addSubjectType")
    @ResponseBody

    public DataResult addSubjectType(@ApiParam(name="试题标签类",value="试题标签对象",required=true) DSubjectType dSubjectType){
        String typeName = dSubjectType.getTypeName();
        if(typeName == null || typeName.equals("")){
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(), "保存失败,试题标签名称不能为空");
        }
        QueryWrapper<DSubjectType> dSubjectTypeQueryWrapper = new QueryWrapper<>();
        dSubjectTypeQueryWrapper.eq("typeName",typeName);
        int count = dSubjectTypeService.count(dSubjectTypeQueryWrapper);
        if(count > 0){
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(), "保存失败,试题标签名称不能重复");
        }

        dSubjectType.setCreateTime(LocalDateTime.now());
        boolean save = dSubjectTypeService.save(dSubjectType);
        if(save == true){
            return DataResult.success();
        }else{
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(), "保存失败");
        }
    }


    @ApiOperation(value = "修改试题标签接口")
    @PutMapping("updateSubjectType")
    @LogAnnotation(title = "基础数据管理", action = "修改试题标签接口")
    @ResponseBody

    public DataResult updateSubjectType(@ApiParam(name="试题标签类",value="对象",required=true) DSubjectType dSubjectType){

        Integer id = dSubjectType.getId();
        if(id == null || id.equals("")){
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(), "修改失败,唯一标识不能为空");
        }
        QueryWrapper<DSubject> dSubjectQueryWrapper = new QueryWrapper<DSubject>();
        dSubjectQueryWrapper.eq("type",id);
        dSubjectQueryWrapper.eq("deleteMark","0");
        int count = dSubjectService.count(dSubjectQueryWrapper);
        if(count > 0){
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(), "修改失败,试题标签下有试题，禁止修改");
        }
        String typeName = dSubjectType.getTypeName();
        if(typeName == null || typeName.equals("")){
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(), "修改失败,试题标签名称不能为空");
        }

        QueryWrapper<DSubjectType> dSubjectTypeQueryWrapper = new QueryWrapper<>();
        dSubjectTypeQueryWrapper.eq("typeName",typeName);
        dSubjectTypeQueryWrapper.ne("id" , id);
        int count1 = dSubjectTypeService.count(dSubjectTypeQueryWrapper);
        if(count1 > 0){
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),  "修改失败,试题标签名称不能重复");
        }

        boolean save = dSubjectTypeService.updateById(dSubjectType);
        if(save == true){
            return DataResult.success();
        }else{
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(), "修改失败");
        }
    }

    @ApiOperation(value = "批量删除试题标签接口")
    @LogAnnotation(title = "基础数据管理", action = "批量删除试题标签接口")
    @DeleteMapping("deleteSubjectType")
    @ResponseBody

    public DataResult deleteBatchSubjectType(@ApiParam(name="唯一标识",value="[1,2,3]",required=true)String  id){
        List ids = JSONArray.parseArray(id);
        if(ids == null || ids.size() <=0){
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(), "删除失败,唯一标识不能为空");
        }
        Integer save = dSubjectTypeService.deleteBatchSubjectType(ids);
        if(save == 1){
           return DataResult.success();
        }else if(save == 3){
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(), "删除失败,试题标签下有试题，禁止删除");
        }else{
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(), "删除失败");
        }
    }


    @ApiOperation(value = "查询试题标签详情")
    @LogAnnotation(title = "基础数据管理", action = "查询试题标签详情")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "唯一标识", name = "id" ,required=true ,  paramType = "query"),
    })
    @GetMapping("selectSubjectTypeById")
    @ResponseBody
    public DataResult selectSubjectTypeById(Integer id){
        HashMap<String, Object> hashMap = new HashMap<>();
        if(id == null || id.equals("")){
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(), "查询失败,唯一标识不能为空");
        }
        DSubjectType byId = dSubjectTypeService.getById(id);
        DataResult success = DataResult.success();
        success.setData(byId);
        return success;
    }


    @ApiOperation(value = "查询试题标签-分页")
    @LogAnnotation(title = "基础数据管理", action = "查询试题标签-分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name="page",value="当前页" ,required=true, paramType = "query"),
            @ApiImplicitParam(name="pageSize",value="每页数量" ,required=true, paramType = "query")
    })
    @GetMapping("selectSubjectTypeIsPage")
    @ResponseBody
    public DataResult selectSubjectTypeById(Integer page ,Integer pageSize ){
        HashMap<String, Object> hashMap = new HashMap<>();
        Page<DSubjectType> dSubjectTypes = new Page<>(page, pageSize);
        IPage<DSubjectType> list = dSubjectTypeService.page(dSubjectTypes);
        DataResult success = DataResult.success();
        success.setData(list);
        return success;
    }

    @ApiOperation(value = "查询试题标签--不分页")
    @GetMapping("selectSubjectTypeIsNotPage")
    @LogAnnotation(title = "基础数据管理", action = "查询试题标签--不分页")
    @ResponseBody
    public DataResult selectSubjectTypeById(){
        HashMap<String, Object> hashMap = new HashMap<>();
        List<DSubjectType> list = dSubjectTypeService.list();
        DataResult success = DataResult.success();
        success.setData(list);
        return success;
    }

}

